// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/common/memory/hash_string_allocator.h>
#include <pollux/vector/base_vector.h>

namespace kumo::pollux::aggregate {
    class ValueSet {
    public:
        explicit ValueSet(HashStringAllocator *allocator) : allocator_(allocator) {
        }

        // Serialize a value in `vector` at `index` to memory allocated via
        // `allocator`. Update `position` to the start address of the written data.
        // Notice that the caller is responsible for freeing up all the space written
        // through this function.
        void write(
            const BaseVector &vector,
            vector_size_t index,
            HashStringAllocator::Position &position) const;

        StringView write(const StringView &value) const;

        // Deserialize the data written in memory at `position` to `vector` at
        // `index`.
        void read(
            BaseVector *vector,
            vector_size_t index,
            const HashStringAllocator::Header *header) const;

        void free(HashStringAllocator::Header *header) const;

        void free(const StringView &value) const;

    private:
        HashStringAllocator *allocator_;
    };
} // namespace kumo::pollux::aggregate
